﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Orange.Mvc4App.Services;
using System.Xml.Linq;
using Orange.Mvc4App.Models;
using Orange.Mvc4App.Infrastructure;

namespace Orange.Mvc4App.Controllers {
    public class ModulesController : BaseController {
        private readonly IModuleService service;

        public ModulesController(IModuleService modulesService) {
            service = modulesService;
        }

        //
        // GET: /Modules/

        public ActionResult Index(int? id, int pageIndex = 1) {
            ViewBag.Id = id;
            ViewBag.category = service.GetList(m => m.BelongTo == null);
            //var module = service.Find(id);
            //if (module != null && module.BelongTo != null) {
            //    return RedirectToAction("Details", new { id = id });
            //}

            return View(new PagedList<Module>(
                    service.GetList(m => m.BelongTo == id),
                    pageIndex,
                    pageSize));
        }

        //Menu
        [ChildActionOnly]
        //[OutputCache(Duration = 1200, VaryByParam = "none")]
        public PartialViewResult Menu() {
            var list = service.GetList(null);
            var model = new List<Menu>();
            foreach (var menu in list.Where(m => m.BelongTo.Equals(null))) {
                List<Tab> tabs = new List<Tab>();
                foreach (var tab in list.Where(
                                    m => m.BelongTo == menu.Id)) {
                    tabs.Add(new Tab {
                        Action = tab.Action,
                        Contoller = tab.Controller,
                        Title = tab.Title
                    });
                }
                model.Add(new Menu { Name = menu.Title, Tabs = tabs });
            }
            return PartialView("_menu", model);
        }

        public ActionResult Details(int id) {
            return View(service.Find(id));
        }

        public ActionResult Create() {
            ViewBag.Parent = service.GetList(m => m.BelongTo == null);
            return View(new Module());
        }

        [HttpPost]
        public ActionResult Create(Module model) {
            if (ModelState.IsValid) {
                service.Add(model);
                service.Save();
                return RedirectToAction("Index");
            }
            IList<Module> par = service.GetList(m => m.BelongTo == null);
            ViewBag.Parent = par;
            return View(model);
        }

        public ActionResult Edit(int id) {
            IList<Module> par = service.GetList(m => m.BelongTo == null);
            ViewBag.Parent = par;
            return View(service.Find(id));
        }

        [HttpPost]
        public ActionResult Edit(Module model) {
            service.Update(model);
            service.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing) {
            if (disposing) {
                service.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
